package com.guigu.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.guigu.health.pojo.CheckItem;
import com.guigu.health.pojo.CheckItemExample;
import com.guigu.health.service.CheckItemService;
import com.mysql.jdbc.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @ClassName:CheckItemController
 * @Description:检查项控制层
 * @Author:longtao
 * @Date:2021/4/14
 * @Version:1.0
 */
@RequestMapping("/checkItem")
@Controller
public class CheckItemController extends BaseController{
    /**
     * 调用checkItemService服务
     */
    @Reference
    private CheckItemService checkItemService;

    /**
     * 模糊查询检查项,并分页
     * @param name
     * @param pageNum
     * @return
     */
    @RequiresPermissions(value = {"CHECKITEM_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/queryAllCheckItem",method = RequestMethod.GET,produces = {"application/json;charset=utf-8"})
    public Object queryAllCheckItem(String name,String pageNum){
        PageInfo<CheckItem> checkItemPageInfo=null;
        if (StringUtils.isNullOrEmpty(pageNum)){
            pageNum="1";
        }
        if (StringUtils.isNullOrEmpty(name)){
           checkItemPageInfo= checkItemService.queryAllCheckItem(null,Integer.valueOf(pageNum),2);
        }else {
            CheckItemExample example=new CheckItemExample();
            CheckItemExample.Criteria criteria=example.createCriteria();
            criteria.andNameLike("%"+name+"%");
            checkItemPageInfo=checkItemService.queryAllCheckItem(example,Integer.valueOf(pageNum),2);
        }
        return checkItemPageInfo;
    }

    /**
     * 删除检查项
     * @param id
     * @return
     */
    @RequiresPermissions(value = {"CHECKITEM_DELETE"})
    @ResponseBody
    @RequestMapping(value = "/deleteCheckItem",method = RequestMethod.GET,produces = {"application/json;charset=utf-8"})
    public Object deleteCheckItem(String id){

        Object b = null;
        try{
            b=checkItemService.deleteCheckItem(Integer.valueOf(id));
        }catch (Exception e){
            b="-997";
            return b;
        }
        return b;
    }

    /**
     * 修改检查项
     * @param checkItem
     * @return
     */
    @RequiresPermissions(value = {"CHECKITEM_EDIT"})
    @ResponseBody
    @RequestMapping(value = "/updateCheckItem",method = RequestMethod.POST,produces = {"application/json;charset=utf-8"})
    public Object updateCheckItem(@RequestBody CheckItem checkItem){
        CheckItemExample example=new CheckItemExample();
        CheckItemExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(Integer.valueOf(checkItem.getId()));
        boolean b = checkItemService.updateCheckItem(checkItem, example);
        return b;
    }

    /**
     * 新增检查项
     * @param checkItem
     * @return
     */
    @RequiresPermissions(value = "CHECKITEM_ADD")
    @ResponseBody
    @RequestMapping(value = "/addCheckItem")
    public Object addCheckItem(@RequestBody CheckItem checkItem){
        return checkItemService.addCheckItem(checkItem);
    }

    /**
     * 判断用户名一致
     * @param code
     * @return
     */
    @RequiresPermissions(value = {"CHECKITEM_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/count")
    public Object count(String code){
        int i = checkItemService.count(code);
        boolean flag=true;
        if (i>0){
            flag=false;
        }
        return flag;
    }
}
